package com.connector.server.server.listener;

import org.slf4j.LoggerFactory;
import com.connector.base.connection.Connection;
import com.connector.server.redis.OnLineUserCache;
import com.connector.base.connection.channel.SocketChannel;
import com.connector.base.connection.channel.SocketIoSocketChannel;
import com.corundumstudio.socketio.SocketIOClient;
import com.connector.base.connection.ConnectionRegistry;
import org.slf4j.Logger;
import com.corundumstudio.socketio.listener.DisconnectListener;

public class SocketIoDisconnectListener implements DisconnectListener
{
    private static final Logger log;
    private final ConnectionRegistry connectionRegistry;
    
    public SocketIoDisconnectListener(final ConnectionRegistry connectionRegistry) {
        this.connectionRegistry = connectionRegistry;
    }
    
    public void onDisconnect(final SocketIOClient socketIOClient) {
        final Connection connection = this.connectionRegistry.removeAndClose((SocketChannel)new SocketIoSocketChannel(socketIOClient));
        if (null != connection && null != connection.getSessionContext()) {
            final String userId = connection.getSessionContext().getUserId();
            OnLineUserCache.del(userId + "_" + connection.getSessionContext().getUuid());
            OnLineUserCache.del(userId);
            SocketIoDisconnectListener.log.info("连接断开:{}", (Object)userId);
        }
        else {
            SocketIoDisconnectListener.log.info("连接断开");
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)SocketIoDisconnectListener.class);
    }
}
